const ADSdk = require("ADSdk");
const UIManage = require("UIManage");
const SoundManage = require("SoundManage");
cc.Class({
    extends: cc.Component,

    properties: {

        curGetMoneyLabel: cc.Label,
        letfMoneyLabel: cc.Label,
        timeLabel: cc.Label,
        closeBtn: cc.Node,
        doubleGetBtn: cc.Node,

    },

    onLoad() {
        this.closeBtn.on("click", this._close, this);
        this.doubleGetBtn.on("click", this._double, this);

        this.maxMoney = Utils.getMaxCash();

        // GameApp.adSdk.showBanner();
    },

    init(moneyNum,hideDouble = false) {
        this.curGetMoney = moneyNum;

        // 数据处理
        this.allMoneyNum = Utils.getRedPacketNum();
        this.allMoneyNum += moneyNum;

        if (this.allMoneyNum >= this.maxMoney) {
            ADSdk.showToast("数据异常");
        } else {
            Utils.addRedPacketNum(this.curGetMoney);
        }

        this.hideDoubleBtn = hideDouble;
        this.showUI();
  
    },

    showUI() {

        this.curGetMoneyLabel.string = this.curGetMoney.toFixed(2) + "元";
        this.letfMoneyLabel.string = "当前余额：" + this.allMoneyNum.toFixed(2) + "元"

        if (this.hideDoubleBtn) {
            this.doubleGetBtn.active = false;
            this.closeBtn.getComponent(cc.Button).interactable = true;
            this.closeBtn.children[0].active = true;
            this.closeBtn.children[1].active = false;
        } else {
            this.doubleGetBtn.active = true;
            this.closeBtn.getComponent(cc.Button).interactable = false;
            this.closeBtn.children[0].active = false;
            this.closeBtn.children[1].active = true;
            this.time = 3;
            this.timeLabel.string = this.time;
            this.showTime();
        }


        if (this.curGetMoney > 90) {
            SoundManage.Instance.playJiaYouSound();
        }

    },


    showTime() {
        this.scheduleOnce(() => {
            this.time--;
            if (this.time <= 0) {
                this.closeBtn.getComponent(cc.Button).interactable = true;
                this.time = 0
                this.closeBtn.children[0].active = true;
                this.closeBtn.children[1].active = false;
            } else {
                this.showTime();
            }
            this.timeLabel.string = this.time;

        }, 1);

    },


    Show() {
        // TODO 广告 插屏
        ADSdk.showInsertAd(5);
        this.node.active = true;
    },

    Close() {
        ADSdk.hideInsertAd();
        this.node.active = false;
        // this.node.removeFromParent()
    },

    _close() {
        // GameApp.adSdk.hideBanner();
        this.showActiveRed();
        this.Close();
    },

    _double() {
        // GameApp.adSdk.hideBanner();
        // TODO 广告 视频
        ADSdk.showVideo(null, (result) => {
            if (result == 1) {
                this.getDoubleSucess();
                this.Close();
            }
        })

    },

    getDoubleSucess(){
        this.allMoneyNum += this.curGetMoney
        if (this.allMoneyNum >= this.maxMoney) {
            ADSdk.showToast("数据异常");
        } else {
            Utils.addRedPacketNum(this.curGetMoney);
            ADSdk.showToast("恭喜你获得双倍红包");
            this._close();
        }
    },


    showActiveRed(){

        let redActiveData = Utils.getRedActive();

        cc.log(redActiveData)
        // cc.log(redActiveData)
        let arr1 = [100, 150, 200, 300];
        let money = Utils.getRedPacketNum();
        for (let i = 0; i < arr1.length; i++) {
            if (redActiveData[i] == false && money >= arr1[i]) {

                cc.log("ShowRedPacketActive")
                UIManage.Instance.ShowRedPacketActive();
            }
        }
    },
    

    // update (dt) {},
});
